package com.srlg.webflux.converter;

import java.time.Instant;

import org.springframework.core.convert.converter.Converter;
import org.springframework.lang.NonNull;

import com.srlg.webflux.entity.Author;
import com.srlg.webflux.entity.Book;

import io.r2dbc.spi.Row;

public class BookConverter implements Converter<Row, Book> {

    @Override
    public Book convert(@NonNull Row source) {
        Book b = new Book();
        b.setId(source.get("id", Long.class));
        b.setTitle(source.get("title", String.class));
        b.setAuthorId(source.get("author_id", Long.class));
        b.setPublishTime(source.get("publish_time", Instant.class));

        Author author = new Author();
        author.setId(author.getId());
        author.setName(source.get("name", String.class));

        b.setAuthor(author);

        return b;
    }

}